import {
  ViewController,
  ITreeExpViewEvent,
  ITreeExpViewState,
  SysUIActionTag,
  ITreeController,
} from '@ibiz-template/runtime';
import { IAppDETreeExplorerView } from '@ibiz/model-core';
import { ExpViewEngine } from './exp-view.engine';

export class TreeExpViewEngine extends ExpViewEngine {
  /**
   * 树导航视图控制器
   */
  protected declare view: ViewController<
    IAppDETreeExplorerView,
    ITreeExpViewState,
    ITreeExpViewEvent
  >;

  /**
   * 树导航栏部件名称
   *
   * @author lxm
   * @date 2023-08-31 03:43:02
   * @readonly
   * @type {string}
   */
  get expBarName(): string {
    return 'treeexpbar';
  }

  /**
   * 树部件控制器
   *
   * @readonly
   * @memberof TreeExpViewEngine
   */
  get tree(): ITreeController {
    return this.expBar.xDataController as ITreeController;
  }

  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
  async call(key: string, args: any): Promise<IData | null | undefined> {
    if (key === SysUIActionTag.REFRESH_ALL) {
      await this.tree.refresh();
      return null;
    }
    // 有数据刷新子节点，没数据刷新所有
    if (key === SysUIActionTag.REFRESH) {
      if (args?.data?.[0]) {
        await this.tree.refreshNodeChildren(args.data[0], false);
      } else {
        await this.tree.refresh();
      }
      return null;
    }
    if (key === SysUIActionTag.REFRESH_PARENT) {
      await this.tree.refreshNodeChildren(args.data[0], true);
      return null;
    }
    return super.call(key, args);
  }
}
